package com.wandian.cqcsk.dialog;

import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.content.Intent;
import android.graphics.Rect;
import android.graphics.drawable.ColorDrawable;
import android.net.Uri;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.mylib.libcore.utils.StringUtil;
import com.mylib.libcore.utils.ToastUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.cqcsk.R;


/**
 * 客服弹框
 */
public class MineKFDialog extends PopupWindow {



    private ImageView iv_close;
    private View mMenuView;
    public MineKFDialog(Context context) {
        super(context);
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mMenuView = inflater.inflate(R.layout.dialog_mine_kf, null);
        iv_close = (ImageView)mMenuView.findViewById(R.id.iv_close);
        iv_close.setOnClickListener(v ->  dismiss());

        TextView tv_qq_btn = (TextView)mMenuView.findViewById(R.id.tv_qq_btn);
        tv_qq_btn.setOnClickListener(v -> {
            if (UIUtils.isQQInstall(context)) {
                final String qqUrl = "mqqwpa://im/chat?chat_type=wpa&uin=948119779";
                context.startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(qqUrl)));
            } else  ToastUtil.show("请安装QQ客户端");
            dismiss();
        });
        TextView tv_wx_btn = (TextView)mMenuView.findViewById(R.id.tv_wx_btn);
        tv_wx_btn.setOnClickListener(v -> {
            try {
                ToastUtil.show("微信号已复制");
                ClipboardManager cm = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
                ClipData mClipData = ClipData.newPlainText("Label", "wxid_hzdy05ss438l22");
                cm.setPrimaryClip(mClipData);
            } catch (Exception e) { }
            dismiss();
        });



        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)this.setClippingEnabled(false);

        //设置SelectPicPopupWindow的View
        this.setContentView(mMenuView);
        //设置SelectPicPopupWindow弹出窗体的宽
        this.setWidth(LayoutParams.FILL_PARENT);
        //设置SelectPicPopupWindow弹出窗体的高
        this.setHeight(LayoutParams.MATCH_PARENT);
        //设置SelectPicPopupWindow弹出窗体可点击
        this.setFocusable(true);
//        //设置SelectPicPopupWindow弹出窗体动画效果
//        this.setAnimationStyle(R.style.popwin_anim_style);
        //实例化一个ColorDrawable颜色为半透明
        ColorDrawable dw = new ColorDrawable(0xb0000000);
        //设置SelectPicPopupWindow弹出窗体的背景
        this.setBackgroundDrawable(dw);


    }

    @Override
    public void showAsDropDown(View anchor) {
        if(Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor);
    }

    @Override
    public void showAsDropDown(View anchor, int xoff, int yoff) {
        if(Build.VERSION.SDK_INT >= 24) {
            Rect rect = new Rect();
            anchor.getGlobalVisibleRect(rect);
            int h = anchor.getResources().getDisplayMetrics().heightPixels - rect.bottom;
            setHeight(h);
        }
        super.showAsDropDown(anchor, xoff, yoff);
    }

}